﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc @Copy Right 2019
// 文件： WxGroupUserRepository.cs
// 项目名称：斑马车务
// 创建时间：2019/10/18
// 负责人：lsc
// ===================================================================
using System;
using System.Text;
using System.Collections.Generic;
using YouWei.Infrastructure.Entity;
using YouWei.Infrastructure.Models;
using Dapper.Extensions;
using Dapper;

namespace YouWei.Infrastructure.Repository
{
	/// <summary>
	/// 仓储层
	/// </summary>	
	public class WxGroupUserRepository:BaseRepository<WxGroupUser>
	{		
    
        #region 构造

        public WxGroupUserRepository() : base(){}

        public WxGroupUserRepository(string connectionString) : base(connectionString){}

        public WxGroupUserRepository(DatabaseContext dbContext) : base(dbContext){}

        #endregion

        /// <summary>
        /// 分页查询，带条件
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="condition"></param>
        /// <returns></returns>
        public Page<WxGroupUserDto> GetPages(int pageIndex, long pageSize, ReqWxGroupUserCondition condition)
        {
            StringBuilder sql = new StringBuilder("SELECT usr.*,wcusr.NickName,wcusr.HeadImgUrl FROM WxGroupUser usr WITH(NOLOCK)INNER JOIN WeChartUser wcusr WITH(NOLOCK)ON usr.WeChartUserID = wcusr.WeChartUserID WHERE usr.IsActive=1");
            if (condition.WxGroupId > 0)
            {
                sql.Append(" AND WxGroupId=@WxGroupId");
            }
            if (!string.IsNullOrWhiteSpace(condition.StartDate))
            {
                sql.Append(" AND CreateDate>@StartDate");
            }
            if (!string.IsNullOrWhiteSpace(condition.EndDate))
            {
                sql.Append(" AND CreateDate<@EndDate");
            }
            if (!string.IsNullOrWhiteSpace(condition.UserName))
            {
                sql.Append(" AND CHARINDEX(@UserName,wcusr.NickName) >0");
            }


            DynamicParameters dparams = new DynamicParameters();
            dparams.Add("@UserName", condition.UserName, System.Data.DbType.String);
            dparams.Add("@StartDate", condition.StartDate, System.Data.DbType.String);
            dparams.Add("@EndDate", condition.EndDate, System.Data.DbType.String);
            dparams.Add("@WxGroupId", condition.WxGroupId, System.Data.DbType.Int32);
            return dbContext.Connection.Page<WxGroupUserDto>(pageIndex, pageSize, sql.ToString(), dparams);
        }

        /// <summary>
        /// 根据用户ID返回所属群组ID列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<int> GetCarGroupIdByUserId(int userId)
        {
            string sql = "SELECT DISTINCT CarGroupID FROM WxGroupUser WHERE UserID = @UserId";
            DynamicParameters dparams = new DynamicParameters();
            dparams.Add("@UserId", userId, System.Data.DbType.Int32);
            return dbContext.Connection.Query<int>(sql, dparams).AsList();
        }

        /// <summary>
        /// 根据用户ID、群组ID是否存在
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public bool IsExistByUserIdAndCarGroupId(int userId,int carGroupId)
        {
            string sql = "SELECT count(1) FROM WxGroupUser where IsActive=1 AND CarGroupID=@CarGroupID AND UserID = @UserId";
            DynamicParameters dparams = new DynamicParameters();
            dparams.Add("@UserId", userId, System.Data.DbType.Int32);
            dparams.Add("@CarGroupID", carGroupId, System.Data.DbType.Int32);
            object obj = dbContext.Connection.ExecuteScalar(sql, dparams);
            if (obj!=null)
            {
                return (int)obj > 0;
            }
            return false;
        }
    }
}